package com.java.controller;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.java.entity.Product;
import com.java.feign.ProductFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

	@Autowired
	private ProductFeignClient productFeignClient;


	@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
	/**
	 *@SentinelResource(blockHandler = "orderBlockHandler",fallback = "orderFallback")
	 * blockHandler 熔断执行的方法
	 * fallback 异常执行的方法
	 * value 资源名称 不设置就是类名加方法名
	 */
	/*@SentinelResource(value ="orderBuyId",blockHandler = "orderBlockHandler",fallback = "orderFallback")*/
	public Product findById(@PathVariable Long id) {
		if(id != 1){
			throw new RuntimeException("错误");
		}
		Product product = null;
		product =productFeignClient.findById(id);
		return product;
	}

	/**
	 * 定义降级逻辑
	 * hystrix和sentinel区别
	 *  hystrix只有一个方法
	 *  sentinel熔断执行熔断的方法、抛出异常执行异常的方法
	 * @param id
	 * @return
	 */
	public Product orderBlockHandler(Long id){
		Product product=new Product();
		product.setProductName("触发熔断的降级方法");
		return product;
	}

	public Product orderFallback(Long id){
		Product product=new Product();
		product.setProductName("触发抛出异常的方法");
		return product;
	}
}
